<?php

/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2019/12/23
 * Time: 14:50
 */
class serv_rapi_publish extends L {

    public static $publishCookie = 'advanced-market=u852o2k7ubhoesbthl5j0siv44; _csrf-market=38f8caf3775d52e12e3d7aea4055cdc0f91afbe7d080212d002e9729ed2d8641a%3A2%3A%7Bi%3A0%3Bs%3A12%3A%22_csrf-market%22%3Bi%3A1%3Bs%3A32%3A%22-hGRrSGa1XcJU3ZBZUh9614ESnsGXRoy%22%3B%7D; goeasyNode=10';

    public static $productCookie = "advanced-products-test=u852o2k7ubhoesbthl5j0siv44";

    public static $pdmCookie = "of_base_language[name]=ZH; of_base_language[name]=ZH; PHPSESSID=v5eh9bbgfs7ivoute2i4ikuepj";

    /**
     * 描述 : 查询刊登数据
     * 参数 :
     *
     * 返回 :
     *
     * 作者 : Zoro.Zhu
     */
    public static function search($params) {
        $url = 'http://product-search.yafex.cn/product-search/ebayonline/search';
        $params['size'] = isset($params['size']) ? $params['size'] : 2000;
        if (empty($params['type'])) {
            $params['listingStatusId'] = [1];                                       //item状态为在线
        }
        unset($params['type']);
        $resule = of_base_com_net::request($url, ['post' => json_encode($params), 'header' => ['Content-Type: application/json;charset=utf-8']]);
        if ($resule['state']) {
            $response = json_decode($resule['response'], true);
            if ($response['ret']) {
                return $response['data'];
            }
        }
        return false;
    }

    /**
     * 描述 : 获取账号等级列表
     * 作者 : Zoro.Zhu
     */
    public static function getAccountGrade($platformId = '') {
        $data = json_encode(array('platformId' => $platformId));
        $subject = 'PLC_getAccountGrade'.$platformId;
        $key = md5($subject);
        if (!empty(of_base_com_kv::get($key))) {
            $data = of_base_com_kv::get($key);
        } else {
            $url = of::config('env.publish');
            $url .= "?c=api_Query&a=findAccountLevel";
            $url = "http://publish.yafex.cn?c=api_Query&a=findAccountLevel";
            $result = of_base_com_net::request($url, array('data' => $data));
            if ($result['state']) {
                $accountGrade = ctrl_com::getPlsConfig('accountGrade');
                $accountGrade = array_flip($accountGrade);
                $response = json_decode($result['response'], true);
                $response = json_decode($response['data'], true);
                $data = [];
                foreach ($response as $row) {
                    $data[$row['account_code'] . $row['site_code']] = $row;
                    $data[$row['account_code'] . $row['site_code']]['gradeId'] = !empty($accountGrade[$row['star_rating']]) ? $accountGrade[$row['star_rating']] : '';
                }
                of_base_com_kv::set($key, $data, 86400);
            }
        }
        return $data;
    }
    
    /**
     * 描述 : 查询刊登数据
     * 参数 :
     *
     * 返回 :
     *
     * 作者 : Zoro.Zhu
     */
    public static function browseExposure($params) {
        $url = of::config('env.publishSearch');
        $url .= "/product-search/ebayonline/scroll";
        $params['size'] = isset($params['size']) ? $params['size'] : 1000;
        if (empty($params['type'])) {
            $params['listingStatusId'] = [1];                                       //item状态为在线
        }
        $resule = of_base_com_net::request($url, ['post' => json_encode($params), 'header' => ['Content-Type: application/json;charset=utf-8']]);
        if ($resule['state']) {
            $response = json_decode($resule['response'], true);
            if ($response['ret']) {
                return $response['data'];
            }
        }
        return false;
    }
    
    /**
     * 描述 : 根据spu查询刊登在线总数据和各站点在线数据
     * 参数 : {
     *         spu ： spu
     *       }
     *
     * 返回 : data : {spu : [
     *          count : 总数
     *          Site : {[
     *                count : 站点在线数
     *          ]}
     *       ]}
     *
     * 作者 : Jack.Hu
     */
    public static function spuOnlineDataSheet($params) {
        $url = of::config('env.publishSearch');
        $url .= "/product-search/ebayonline/groupByMainSkuAndSite";
        $resule = of_base_com_net::request($url, ['post' => json_encode($params), 'header' => ['Content-Type: application/json;charset=utf-8']]);
        if ($resule['state']) {
            $response = json_decode($resule['response'], true);
            if ($response['ret']) {
                return $response['data']['aggsMap'];
            }
        }
        return false;
    }

    /**
     * 描述 : 获取ITEM信息
     * 作者 : Zoro.Zhu
     */
    public static function getEbayItemInfo($itemId,$type = 1){
        $url = "http://ebay.yafex.cn/index.php?r=ebay_listing/listing/get-list&debug=true";
        $post = [
            'order' => 'asc',
            'offset' => '0',
            'limit' => '10',
            'type' => $type,
            'flag_table' => '1',
            'item_id' => $itemId,
            'account' => '',
            'hidden_account' => '',
            'site' => '',
            'hidden_site' => '',
            'sale' => '',
            'hidden_sale' => '',
            'main_sku' => '',
            'goods_sku' => '',
            'hidden_search' => '',
            'new_item_id' => '0',
            'oms_item_id' => '',
        ];
        $result = of_base_com_net::request($url, ['post' => $post,
            'cookie' => self::$publishCookie,
        ]);
        if(!empty($result['state'])){
            if($result['response'] == '[]' && $type < 3){
                return self::getEbayItemInfo($itemId, $type +1);
            }
            $response = json_decode($result['response'], true);
            return $response;
        }
        return false;
    }

    /**
     * 描述 : 获取ITEM信息
     * 作者 : Zoro.Zhu
     */
    public static function getWishItemInfo($itemId,$type = 1){
        $url = "http://market.yafex.cn/index.php?r=wish_listing/listing/get-list&debug=true";
        $post = [
            'order' => 'asc',
            'offset' => '0',
            'limit' => '10',
            'type' => $type,
            'flag_table' => '1',
            'item_id' => $itemId,
            'account' => '',
            'hidden_account' => '',
            'site' => '',
            'hidden_site' => '',
            'sale' => '',
            'hidden_sale' => '',
            'main_sku' => '',
            'goods_sku' => '',
            'hidden_search' => '',
            'new_item_id' => '0',
            'oms_item_id' => '',
        ];
        $result = of_base_com_net::request($url, ['post' => $post,
            'cookie' => self::$publishCookie,
        ]);
        if(!empty($result['state'])){
            if($result['response'] == '[]' && $type < 2){
                return self::getWishItemInfo($itemId, $type +1);
            }
            $response = json_decode($result['response'], true);
            return $response;
        }
        return false;
    }

    /**
     * 描述 : 获取SKU
     * 作者 : Zoro.Zhu
     */
    public static function getSku($mainSku){
        $url = "http://products.yafex.cn/index.php?r=goods/goods-sku/get-list&session_id=u852o2k7ubhoesbthl5j0siv44";
        $post = [
            'sort' => "0",
            'order' => "DESC",
            'offset' => "0",
            'limit' => "10",
            'sku_info1' => "sku",
            'sku1' => $mainSku,
            'action' => "",
            'condition1_str' => "",
            'condition1_end' => "",
            'with_or' => "1",
            'sku_info2' => "fuzzy",
            'sku2' => "",
            'condition2_str' => "",
            'condition2_end' => "",
            'sell_user_id' => "0",
            'dev_user_id' => "0",
            'edit_user_id' => "0",
            'purchase_user_id' => "0",
            'maintain_user_id' => "0",
            'supplier_dev_user_id' => "0",
            'status' => "0",
            'status_platform' => "1",
            'is_tort' => "-1",
            'tort_platform' => "1",
            'focus_promotion' => "-1",
            'is_sync_mb' => "-1",
            'brand_id' => "0",
            'star' => "0",
            'platform_star' => "0",
            'star_platform' => "0",
            'is_upload_pic' => "-1",
            'source_code' => "0",
            'hidden_goods_type' => "",
            'goods_type' => "",
            'festival_holiday' => "-1",
            'is_seasonal' => "-1",
            'category_1' => "0",
            'category_2' => "0",
            'category_3' => "0",
            'category_4' => "0",
            'category_5' => "0",
            'category_6' => "0",
            'fmcg_category_1' => "0",
            'fmcg_category_2' => "0",
            'fmcg_category_3' => "0",
            'fmcg_category_4' => "0",
            'fmcg_category_5' => "0",
            'fmcg_category_6' => "0",
            'assist_category_1' => "0",
            'assist_category_2' => "0",
            'assist_category_3' => "0",
            'assist_category_4' => "0",
            'assist_category_5' => "0",
            'assist_category_6' => "0",
            'created_time_start' => "",
            'created_time_end' => "",
            'done_date_start' => "",
            'done_date_end' => "",
            'hidden_warehouse' => "",
            'warehouse' => "",
            'goods_label' => "0",
            'day_sales' => "0",
            'min_number' => "",
            'max_number' => "",
            'has_selfie' => "-1",
            'language_id' => "0",
            'new_old' => "0",
            'is_recheck' => "-1",
            'is_virtual_oversea' => "-1",
            'product_dev_assistant' => "0",
            'hidden_is_charge' => "",
            'is_charge' => "",
            'promotion_platform' => "0",
            'block_publish' => "-1",
            'is_can_sale' => "-1",
            'cms_cloth_style' => "-1",
            'cms_planning_time' => "-1",
            'cms_specific_holiday' => "-1",
            'amazon_cloth_type' => "0",
            'live_picture' => "-1",
            'three_dimensional' => "-1",
            'fmcg_category_path' => "",
            'assist_category_path' => "",
        ];
        $result = of_base_com_net::request($url, ['post' => $post,
            'cookie' => self::$productCookie,
        ]);
        if(!empty($result['state'])){
            $response = json_decode($result['response'], true);
            return $response;
        }
        return false;
    }

    /**
     * 描述 : 获取商品详情
     * 作者 : Zoro.Zhu
     */
    public static function getSkuDetail($id,$itemId){
        $url = "http://products.yafex.cn/index.php?r=goods/goods/item-sku-info&id={$id}&item_id={$itemId}";
        $result = of_base_com_net::request($url, ['get' => '',
            'cookie' => self::$productCookie,
        ]);
        if($result['state']){
            $html = $result['response'];
            $xx = [
                'title' => [
                    'xPath' => '//*[@id="product-attr"]/div[1]/table/thead/tr/th',
                ],
            ];
            $xxP = core_xtmDec::xPath($html, $xx);
            $l = [];
            foreach($xxP['title'] as $k => $r){
                switch ($r['content']) {
                    case "SKU编号":
                        $l['sku'] = $k + 1;
                        break;
                    case "重量(g)":
                        $l['weight'] = $k + 1;
                        break;
                    case "采购价格(￥)":
                        $l['price'] = $k + 1;
                        break;
                    case "建议售价($)":
                        $l['shopPrice'] = $k + 1;
                        break;
                    case "原厂编号(*)":
                        $l['oldNumber'] = $k + 1;
                        break;
                    case "包裹内容":
                        $l['packing'] = $k + 1;
                        break;
                    case "长宽高(cm)":
                        $l['lwh'] = $k + 1;
                        break;
                }
            }
            $xPaths = array(
                //报关中文名
                'nameCn' => array(
                    'xPath' => '//*[@id="tab_1"]/form/div/div/div[1]/div[1]/div[10]/div',
                ),
                //报关英文名
                'nameEn' => array(
                    'xPath' => '//*[@id="tab_1"]/form/div/div/div[1]/div[1]/div[11]/div',
                ),
                //物品属性
                'productAttr' => array(
                    'xPath' => '//*[@id="tab_1"]/form/div/div/div[2]/div[1]/div[11]/div',
                ),
                //SKU
                'sku' => array(
                    'xPath' => '//*[@id="product-attr"]/div[1]/table/tbody/tr/td[' . $l['sku'] . ']',
                ),
                //重量
                'weight' => array(
                    'xPath' => '//*[@id="product-attr"]/div[1]/table/tbody/tr/td[' . $l['weight'] . ']',
                ),
                //采购价
                'price' => array(
                    'xPath' => '//*[@id="product-attr"]/div[1]/table/tbody/tr/td[' . $l['price'] . ']',
                ),
                //建议售价($)
                'shopPrice' => array(
                    'xPath' => '//*[@id="product-attr"]/div[1]/table/tbody/tr/td[' . $l['shopPrice'] . ']',
                ),
                //原厂编号
                'oldNumber' => array(
                    'xPath' => '//*[@id="product-attr"]/div[1]/table/tbody/tr/td[' . $l['oldNumber'] . ']',
                ),
                //包裹内容
                'packing' => array(
                    'xPath' => '//*[@id="product-attr"]/div[1]/table/tbody/tr/td[' . $l['packing'] . ']',
                ),
                //长宽高(cm)
                'lwh' => array(
                    'xPath' => '//*[@id="product-attr"]/div[1]/table/tbody/tr/td[' . $l['lwh'] . ']',
                ),
                //供应商编码
                'sellerCode' => array(
                    'xPath' => '//*[@id="tab_6"]/form/table/tbody/tr/td[2]',
                ),
                //供应商名称
                'sellerName' => array(
                    'xPath' => '//*[@id="tab_6"]/form/table/tbody/tr/td[3]',
                ),
                //供应商链接
                'sellerUrl' => array(
                    'xPath' => '//*[@id="tab_6"]/form/table/tbody/tr/td[4]',
                ),
                //报价
                'sellerPrice' => array(
                    'xPath' => '//*[@id="tab_6"]/form/table/tbody/tr/td[6]',
                ),
                //最小起订量
                'minQty' => array(
                    'xPath' => '//*[@id="tab_6"]/form/table/tbody/tr/td[7]',
                ),
                //采购链接
                'purchaseUrl' => array(
                    'xPath' => '//*[@id="tab_1"]/form/div/div/div[2]/div[2]/div[18]/div/div/a',
                ),
                //备用采购链接
                'purchaseUrl2' => array(
                    'xPath' => '//*[@id="tab_1"]/form/div/div/div[2]/div[2]/div[19]/div/div/a',
                ),
            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $xPathRe = removeBlank($xPathRe);
            $purchaseUrl = [];
            foreach($xPathRe['purchaseUrl'] as $row){
                $purchaseUrl[] = $row['content'];
            }
            foreach($xPathRe['purchaseUrl2'] as $row){
                $purchaseUrl[] = $row['content'];
            }
            $skuAttr = [];
            foreach($xPathRe['sku'] as $key => $row){
                $skuAttr[$key]['sku'] = $row['content'];
                $skuAttr[$key]['weight'] = $xPathRe['weight'][$key]['content'];
                $skuAttr[$key]['price'] = $xPathRe['price'][$key]['content'];
                $skuAttr[$key]['shopPrice'] = $xPathRe['shopPrice'][$key]['content'];
                $skuAttr[$key]['oldNumber'] = $xPathRe['oldNumber'][$key]['content'];
                $skuAttr[$key]['packing'] = $xPathRe['packing'][$key]['content'];
                $skuAttr[$key]['lwh'] = $xPathRe['lwh'][$key]['content'];
            }
            $seller = [];
            foreach($xPathRe['sellerCode'] as $key => $row){
                $seller[$key]['sellerCode'] = $row['content'];
                $seller[$key]['sellerName'] = $xPathRe['sellerName'][$key]['content'];
                $seller[$key]['sellerUrl'] = $xPathRe['sellerUrl'][$key]['content'];
                $seller[$key]['sellerPrice'] = $xPathRe['sellerPrice'][$key]['content'];
                $seller[$key]['minQty'] = $xPathRe['minQty'][$key]['content'];
            }
            $data = [
                'nameCn' => $xPathRe['nameCn'][0]['content'],
                'nameEn' => $xPathRe['nameEn'][0]['content'],
                'productAttr' => $xPathRe['productAttr'][0]['content'],
                'purchaseUrl' => $purchaseUrl,
                'skuAttr' => $skuAttr,
                'seller' => $seller,
            ];
            return $data;
        }
        return false;
    }
    /**
     * 描述 : 获取产品SKU信息
     * 作者 : Zoro.Zhu
     */
    public static function getPdmSkuInfo($sku){
        $url = "https://pdm.yafex.cn/?c=goods_sku&a=viewSkuAttr&skuId={$sku}";
        $res = of_base_com_net::request($url, ['type' => 'get', 'cookie' => self::$pdmCookie,]);
        if($res['state']){
            $html = $res['response'];
            $xx = [
                'json' => [
                    'xPath' => '//*[@name="skuAttrJson"]/@value',
                ],
            ];
            $xxP = core_xtmDec::xPath($html, $xx);
            $json = json_decode(stripslashes($xxP['json'][0]['content']), true);
            return $json;
        }
        return false;
    }
    /**
     * 描述 : 获取产品SPU信息
     * 作者 : Zoro.Zhu
     */
    public static function getPdmSpuInfo($sku){
        $url = "https://pdm.yafex.cn/?c=goods_sku&a=viewSpuAttr&skuId={$sku}";
        $res = of_base_com_net::request($url, ['type' => 'get', 'cookie' => self::$pdmCookie,]);
        if($res['state']){
            $html = $res['response'];
            $xx = [
                'json' => [
                    'xPath' => '//*[@name="skuAttrJson"]/@value',
                ],
            ];
            $xxP = core_xtmDec::xPath($html, $xx);
            $json = json_decode(stripslashes($xxP['json'][0]['content']), true);
            return $json;
        }
        return false;
    }
    /**
     * 描述 : 获取供应商信息
     * 作者 : Zoro.Zhu
     */
    public static function getPdmSellerInfo($sku){
        $url = "https://pdm.yafex.cn/?c=goods_sku&a=viewSkuSeller&skuId={$sku}";
        $res = of_base_com_net::request($url, ['type' => 'get', 'cookie' => self::$pdmCookie,]);
        if($res['state']){
            $html = $res['response'];
            $xx = [
                //供应商编码
                'sellerCode' => array(
                    'xPath' => '//tr/td[1]',
                ),
                //供应商名称
                'sellerName' => array(
                    'xPath' => '//tr/td[1]',
                ),
                //供应商链接
                'sellerUrl' => array(
                    'xPath' => '//tr/td[10]/a/text()',
                ),
                //报价
                'sellerPrice' => array(
                    'xPath' => '//tr/td[6]',
                ),
                //最小起订量
                'minQty' => array(
                    'xPath' => '//tr/td[8]',
                ),
            ];
            $xPathRe = core_xtmDec::xPath($html, $xx);
            $seller = [];
            foreach($xPathRe['sellerCode'] as $key => $row){
                $seller[$key]['sellerCode'] = $row['content'];
                $seller[$key]['sellerName'] = $xPathRe['sellerName'][$key]['content'];
                $seller[$key]['sellerUrl'] = $xPathRe['sellerUrl'][$key]['content'];
                $seller[$key]['sellerPrice'] = !empty($xPathRe['sellerPrice'][$key]['content']) ? $xPathRe['sellerPrice'][$key]['content'] / 100 : '';
                $seller[$key]['minQty'] = $xPathRe['minQty'][$key]['content'];
            }
            return $seller;
        }
        return false;
    }
    /**
     * 描述 : 获取产品SPU属性信息
     * 作者 : Zoro.Zhu
     */
    public static function getPdmSpuAttrInfo($spu){
        $url = "https://pdm.yafex.cn/?c=goods_sku&a=viewProductAttr&spu={$spu}";
        $res = of_base_com_net::request($url, ['type' => 'get', 'cookie' => self::$pdmCookie,]);
        if($res['state']){
            $html = $res['response'];
//            echo $html;exit;
            $xx = [
                'title' => [
                    'xPath' => '//*/tr/th',
                ],
            ];
            $xxP = core_xtmDec::xPath($html, $xx);
//            p($xxP);exit;
            $l = [];
            foreach($xxP['title'] as $k => $r){
                switch ($r['content']) {
                    case "SKU编码":
                        $l['sku'] = $k + 1;
                        break;
                    case "开发重量(g)":
                        $l['weight'] = $k + 1;
                        break;
                    case "申报价值":
                        $l['price'] = $k + 1;
                        break;
                    case "建议售价($)":
                        $l['shopPrice'] = $k + 1;
                        break;
                    case "原厂编号(*)":
                        $l['oldNumber'] = $k + 1;
                        break;
                    case "包裹内容":
                        $l['packing'] = $k + 1;
                        break;
                    case "是否强制质检":
                        $l['lwh'] = $k + 2;
                        break;
                }
            }
            $xPaths = array(
                //SKU
                'sku' => array(
                    'xPath' => '//*/tr/td[' . $l['sku'] . ']',
                ),
                //重量
                'weight' => array(
                    'xPath' => '//*/tr/td[' . $l['weight'] . ']/input/@value',
                ),
                //采购价
                'price' => array(
                    'xPath' => '//*/tr/td[' . $l['price'] . ']/input/@value',
                ),
                //建议售价($)
                'shopPrice' => array(
                    'xPath' => '//*/tr/td[' . $l['shopPrice'] . ']/input/@value',
                ),
                //原厂编号
                'oldNumber' => array(
                    'xPath' => '//*/tr/td[' . $l['oldNumber'] . ']',
                ),
                //包裹内容
                'packing' => array(
                    'xPath' => '//*/tr/td[' . $l['packing'] . ']',
                ),
                //长宽高(cm)
                'lwh' => array(
                    'xPath' => '//*/tr/td[' . $l['lwh'] . ']/input/@value',
                ),
            );
            $xPathRe = core_xtmDec::xPath($html, $xPaths);
            $xPathRe = removeBlank($xPathRe);
            $skuAttr = [];
            foreach($xPathRe['sku'] as $key => $row){
                $skuAttr[$key]['sku'] = $row['content'];
                $skuAttr[$key]['weight'] = $xPathRe['weight'][$key]['content'];
                $skuAttr[$key]['price'] = $xPathRe['price'][$key]['content'];
                $skuAttr[$key]['shopPrice'] = $xPathRe['shopPrice'][$key]['content'];
                $skuAttr[$key]['oldNumber'] = $xPathRe['oldNumber'][$key]['content'];
                $skuAttr[$key]['packing'] = $xPathRe['packing'][$key]['content'];
                $skuAttr[$key]['lwh'] = $xPathRe['lwh'][$key]['content'];
            }
            return $skuAttr;
        }
        return false;
    }
    /**
     * 描述 : 获取shopee信息
     * 作者 : Zoro.Zhu
     */
    public static function getShopeeItemInfo($account,$id){
        $url = "http://market.yafex.cn/index.php?r=shopee_online/product/get-list&order=asc&offset=0&limit=10&flag_table=10&item_id={$id}&account={$account}&site_code=&sales_id=&goods_status=&goods_status_id=&main_sku=&publish_sku=&title=&time_value_low=&time_value_height=&sales_start=&sales_end=&profit_rate_start=&profit_rate_end=&sku_list=1&show-all-sku=on";
        $post = [
            'order' => 'asc',
            'offset' => '0',
            'limit' => '10',
            'flag_table' => '10',
            'item_id' => $id,
            'account' => $account,
            'site_code' => '',
            'sales_id' => '',
            'goods_status' => '',
            'goods_status_id' => '',
            'main_sku' => '',
            'publish_sku' => '',
            'title' => '',
            'time_value_low' => '',
            'time_value_height' => '',
            'sales_start' => '',
            'sales_end' => '',
            'profit_rate_start' => '',
            'profit_rate_end' => '',
            'sku_list' => '1',
            'show-all-sku' => 'on',
        ];
        $result = of_base_com_net::request($url, ['get' => $post,
            'cookie' => self::$publishCookie,
        ]);
        if(!empty($result['state'])){
            if($result['response'] == '[]'){
                return false;
            }
            $response = json_decode($result['response'], true);
            return $response;
        }
        return false;
    }
}